#include "gui_rsrc.h"

/* user_img 描述信息结构体 */
typedef struct IMG_RSRC
{
    lv_img_dsc_t  *img;            /* 图片变量的指针 */
    const char    *file_name;      /* 文件名 */
} img_rsrc_t;


/* 桌面壁纸变量定义 */
lv_img_dsc_t wallpaper_img;

/* 桌面图片变量定义 */
/* page1 */
lv_img_dsc_t icon_led_main;
lv_img_dsc_t icon_axis_main;
lv_img_dsc_t icon_camera_main;
lv_img_dsc_t icon_clock_main;
lv_img_dsc_t icon_set_main;
lv_img_dsc_t icon_picture_main;
lv_img_dsc_t icon_game_main;
/* page2 */
lv_img_dsc_t icon_caculator_main;
/* page3 */
lv_img_dsc_t icon_compass_main;

/* 应用app图片变量定义 */
/* ------ page1 ------ */

/* dial */
lv_img_dsc_t icon_dial_main_clock;
lv_img_dsc_t icon_dial_weather;
lv_img_dsc_t icon_dial_step;
lv_img_dsc_t icon_dial_heart;
lv_img_dsc_t icon_dial_oxygen;
lv_img_dsc_t icon_dial_music;
lv_img_dsc_t icon_dial_camera;
lv_img_dsc_t icon_dial_compass;
lv_img_dsc_t icon_dial_set;
lv_img_dsc_t icon_dial_wp_sona;
lv_img_dsc_t icon_dial_wp_zed;

/* 桌面壁纸资源管理 */
const img_rsrc_t wallpaper_img_rsrc[] =
{
    {&wallpaper_img, "0:lvgl/wallpaper/caoyuan.bin"},
    {&wallpaper_img, "0:lvgl/wallpaper/huacao.bin"},
    {&wallpaper_img, "0:lvgl/wallpaper/qiqiu.bin"},
    {&wallpaper_img, "0:lvgl/wallpaper/xingkong_h.bin"},
    {&wallpaper_img, "0:lvgl/wallpaper/dva1_h.bin"}
};

/* 桌面图标资源管理 */
static const img_rsrc_t page_icon_rsrc[] =
{
    /* page1 */
    {&icon_led_main, "0:lvgl/app/led/icon/leds.bin"},
    {&icon_axis_main, "0:lvgl/app/camera/icon/camera.bin"},
    {&icon_camera_main, "0:lvgl/app/camera/icon/camera.bin"},
    {&icon_clock_main, "0:lvgl/app/clock/icon/clock.bin"},
    {&icon_set_main, "0:lvgl/app/set/icon/sets.bin"},
    {&icon_picture_main, "0:lvgl/app/picture/icon/picture.bin"},
    {&icon_game_main, "0:lvgl/app/game/icon/game.bin"},

    /* page2 */
    {&icon_caculator_main, "0:lvgl/app/caculator/icon/caculator.bin"},

    /* page3 */
    {&icon_compass_main, "0:lvgl/app/compass/icon/compass.bin"},
};

/* 应用内图标资源管理 */
static const img_rsrc_t app_icon_rsrc[] =
{
    /* ------ page1 ------ */

    /* dial */
    {&icon_dial_main_clock, "0:lvgl/app/dial/app/main_clock.bin"},
    {&icon_dial_weather, "0:lvgl/app/dial/app/weather.bin"},
    {&icon_dial_step, "0:lvgl/app/dial/app/step.bin"},
    {&icon_dial_heart, "0:lvgl/app/dial/app/heart.bin"},
    {&icon_dial_oxygen, "0:lvgl/app/dial/app/oxygen.bin"},
    {&icon_dial_music, "0:lvgl/app/dial/app/music.bin"},
    {&icon_dial_camera, "0:lvgl/app/dial/app/camera.bin"},
    {&icon_dial_compass, "0:lvgl/app/dial/app/compass.bin"},
    {&icon_dial_set, "0:lvgl/app/dial/app/sets.bin"},
    {&icon_dial_wp_sona, "0:lvgl/app/dial/app/sona_1.bin"},
    {&icon_dial_wp_zed, "0:lvgl/app/dial/app/zed_1.bin"}
};

/**
* @brief 从文件系统加载字体
* @param None
* @retval   0-加载成功 非0-加载失败
*/
RET_STATUS gui_rsrc_load_font_from_file(void)
{
    return RET_OK;
}

/**
* @brief 从文件系统获取bmp图片
* @param img-存储图片信息的指针
* @param file_name-文件名
* @retval   0-加载成功 非0-加载失败
*/
RET_STATUS gui_rsrc_load_img_bin_from_file(lv_img_dsc_t *img, const char *file_name)
{
    uint8_t  res;
    uint32_t byte_nbr;
    uint8_t *pbuf;        //读取数据缓冲区
    uint8_t *pbmp;
    uint32_t img_header;
    uint32_t file_size;

    /* 参数校验 */
    if ((img == NULL) || (file_name == NULL))
    {
        return RET_ERROR;
    }

    /* 获得文件对象 (对应不同的存储设备) */
    FS_FIL *fs_file_obj = fs_user_get_fileobj(FS_DEV_SD);

    if (fs_file_obj == NULL)
    {
        return RET_ERROR;
    }

    /* 打开指定路径下的文件 */
    res = f_open(fs_file_obj, (const TCHAR*) file_name, FA_OPEN_EXISTING | FA_READ);

    if (res != FR_OK)
    {
        return RET_ERROR;
    }

    file_size = fs_file_obj->obj.objsize;

    /* 检查图片最大尺寸 */
    if (file_size > GUI_RSRC_IMG_MAX_SIZE)
    {
        /* 文件过大了 */
        f_close(fs_file_obj);
        return RET_ERROR;
    }

    /* 为图片申请内存, 如果图片需要一直显示, 内存不用释放 */
    pbuf = lv_mem_alloc(file_size);

    if (pbuf == NULL)
    {
        return RET_ERROR;
    }

    /* 将文件读到pbuf */
    res = f_read(fs_file_obj, pbuf, file_size, &byte_nbr);
    if ((res != FR_OK) || (byte_nbr == 0))
    {
        f_close(fs_file_obj);
        return RET_ERROR;
    }

    /* 获取图片信息头 */
    img_header =  ((uint32_t) pbuf[3]) << 24;
    img_header |= ((uint32_t) pbuf[2]) << 16;
    img_header |= ((uint32_t) pbuf[1]) << 8;
    img_header |= ((uint32_t) pbuf[0]);

    /* 获取图片数据流 */
    pbmp = (uint8_t *) pbuf + 4;

    /* 赋值 lv_img_dsc_t */
    img->header.cf          = pbuf[0];   /* Color format */
    img->header.always_zero = 0;
    img->header.w           = (uint16_t) (img_header >> 10);    /* Width of the image map */
    img->header.h           = (uint16_t) (img_header >> 21);    /* Height of the image map */
    img->data_size          = file_size;
    img->data               = pbmp;

    return RET_OK;
}

/**
* @brief 从文件系统加载所有图片到内存
* @param None
* @retval   0-加载成功 非0-加载失败
*/
RET_STATUS gui_rsrc_load_img_from_file(void)
{
    uint16_t i = 0;

    /* 加载默认桌面壁纸图片 */
    if (gui_rsrc_load_img_bin_from_file(wallpaper_img_rsrc[GUI_RSRC_WALLPAPER_DEF].img, 
                                        wallpaper_img_rsrc[GUI_RSRC_WALLPAPER_DEF].file_name) != RET_OK)
    {
        return RET_ERROR;
    }
    
    /* 加载应用图标 */
    for (i = 0; i < (sizeof(page_icon_rsrc) / sizeof(img_rsrc_t)); i++)
    {
        if (gui_rsrc_load_img_bin_from_file(page_icon_rsrc[i].img, 
                                            page_icon_rsrc[i].file_name) != RET_OK)
        {
            return RET_ERROR;
        }
    }

    /* 加载应用内图标 */
    for (i = 0; i < (sizeof(app_icon_rsrc) / sizeof(img_rsrc_t)); i++)
    {
        if (gui_rsrc_load_img_bin_from_file(app_icon_rsrc[i].img, 
                                            app_icon_rsrc[i].file_name) != RET_OK)
        {
            return RET_ERROR;
        }
    }

    return RET_OK;
}
